// try {
v='v1.0.1'
window.onerror = function(message,url,line){//两边都放以防script error错误
    // 汇报错误
    document.getElementById("cc").innerHTML =v+"执行" + url + "文件中的第" + line + "行代码出错，错误信息：" + message;
    window.onerror = null;
    // 不希望此错误继续扩散
    return true;
};
    var pop=null//歌曲列表
var isrotate=1//旋转状态，0竖
var txt = document.getElementById("txtSearch");
//初始化收藏
initCollect=()=> {
    var collects
    collects= collect.split('^');
    var cc = document.getElementById("cc");
    cc.innerHTML=""
    for (i = 0; i < collects.length; i++) {
        var li = document.createElement("li");
        cc.appendChild(li);
        li.innerText = collects[i];
        li.setAttribute('music',getArrayIndex(names,collects[i]))
    }
    cc.onclick = l => {
        l = l.toElement;
        txt.innerText = l.innerText;
        music = l.getAttribute('music')
        changeCollect.innerText='取消收藏'
    }
}
initCollect()
var changeCollect=document.getElementById('changeCollect')
changeCollect.onclick = function () {//修改收藏
    if(music<2)return;
    let m=names[music]
    let n = collect.indexOf(m);
    n==-1? collect+='^'+m :collect=n==0? collect.substr(m.length+1) : collect.replace('^'+m,'')
    changeCollect.innerText=changeCollect.innerText=='加入收藏'?'取消收藏':'加入收藏';
    initCollect()
}
//返回下标
function getArrayIndex(arr, obj) {
    var i = arr.length;
    while (i--) {
        if (arr[i] === obj) {
            return i;
        }
    }
    return -1;
}

//初始化音乐列表
openClose=e=> {
    e.style.display == "none"?showE(e):hideE(e)
}
initMusic=()=>{
    pop = document.createElement("div");
    pop.id = "pop";
    box.appendChild(pop);
    var ul = document.createElement("ul");
    pop.appendChild(ul);
    //2.2根据数组中的每一项数据创建li
    for (var i = 0; i < names.length; ++i) {
        var li = document.createElement("li");
        ul.appendChild(li);
        li.innerText = names[i];
        namesPY[i]=pinyin.getCamelChars(names[i])
        li.setAttribute('music',i)
        li.id=i;
    }
    ul.onclick=l=>{
        l=l.toElement;
        if(l.localName!='li')return
        txt.innerText=l.innerText;
        music=l.getAttribute('music');
        changeCollect.innerText=collect.indexOf(music-1)>-1?'取消收藏':'加入收藏';
        openClose(pop)
    }
    openClose(pop)
}
showE=e=>{(e.style.display != "block")&&(e.style.display = "block")};
hideE=e=>{(e.style.display != "none")&&(e.style.display = "none")};
showAll=()=>{var li = document.getElementsByTagName("li");
    try{for(var e in li)showE(li[e])}catch (e){}};
initMusic()
//1.用户输入的同时 从datas中 寻找符合要求的 项目 放到一个新数组中
input = function () {
    var val = txt.innerText;//获取当前文本框中的内容
    //没内容全显
    if (""==val) {
        showAll();
        return;
    }else{
        if(val.endsWith('\n\n')){
            val=txt.innerText=val.trim();
            txt.blur()
        }
    }
    var li = document.getElementsByTagName("li");
    //        try{for(var e in li)hideE(li[e])}catch (e){}
    showE(pop);
    var have=false;
    for (var i = 0; i < names.length; i++) {
        //判断每一个歌名是否包含val
        if (names[i].indexOf(val) > -1||namesPY[i].indexOf(val.toUpperCase()) > -1) {//符合要求
            showE(document.getElementById(i));
            have=true
        }else{
            hideE(document.getElementById(i))
        }
    }
    if (!have) {//一个都没，不显
        hideE(pop)
    }
};
//输入监听
txt.addEventListener("input", input);
txt.onclick=()=>{if(txt.innerText=='随机播放'||txt.innerText=='播放全部')txt.innerText=""};
txt.ondblclick=()=>txt.innerText=''
txt.innerText=names[music]
let isChecked="";
initChecked=()=>{
    var radioList = document.getElementsByName('item');
    for (var i = 0, len = radioList.length; i < len; i++) {
        if(isChecked.indexOf(i)>-1){
            radioList[i].checked =true
        }
    }
};
document.getElementsByClassName('combo-arrow')[0].onclick=()=>{openClose(pop);showAll();txt.innerText=""}
checked=()=>{
    let str=' ';
    var radioList = document.getElementsByName('item');
    for (var i = 0, len = radioList.length; i < len; i++) {
        str+=radioList[i].checked ?i:""
    }
    return str
};

function actions(data) {//数据提交 post 请求
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "/set_event", true);
    xhr.setRequestHeader("Content-type", "application/json");
    xhr.onreadystatechange = function () {
        var XMLHttpReq = xhr;
        if (XMLHttpReq.readyState == 4) {
            if (XMLHttpReq.status == 200) {
                var text = XMLHttpReq.responseText;
                console.log(text);
            } else {
                console.log(XMLHttpReq.status)
            }
        } else {
            console.log(XMLHttpReq.readyState);
        }
    };
    //alert('{"name": "showUI_commit","value":' + data + '}')
    xhr.send('{"name": "showUI_commit","value":"' + data + '"}');
}



document.getElementById('actions').onclick = function () {//数据提交按钮点击
    let c=checked();
    let b=document.getElementById("select").value;
    actions(music+','+c+','+b+','+collect);
};

document.getElementById('no').onclick = function () {//转屏
    isrotate?noRotate():rotate()
}
con = document.getElementById('c');
width = document.documentElement.clientWidth;
height = document.documentElement.clientHeight;

function rotate() {
    con.style.width = height + 'px';
    con.style.height = width + 'px';
    con.style.top = (height - width) / 2 + 'px';
    con.style.left = 0 - (height - width) / 2 + 'px';
    con.style.transform = loadRotate==2?'rotate(270deg)':'rotate(90deg)';
    isrotate=1
}
rotate()
function noRotate() {
    con.style.width = width + 'px';
    con.style.height = height + 'px';
    con.style.top = '0px';
    con.style.left = '0px';
    con.style.transform = 'none';
    isrotate=0
}
function changeRotate() {
    loadRotate=loadRotate==2?1:2
    isrotate&&rotate()
}
// }catch(err) {
//     document.getElementById("cc").innerHTML = '错误：'+err.message;
// }